﻿//-----------------------------------------------------------------------
// <copyright file="EntitySnapshot.cs" company="billfellows.net">
//     I mention copyright so StyleCop is happy.
// </copyright>
//-----------------------------------------------------------------------
namespace ReputationMinder
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /// <summary>
    /// This provides a mapping between the database table and a .net object.
    /// </summary>
    public class EntitySnapshot
    {
        /*
            CREATE TABLE 
                dbo.EntitySnapshot
            (
                EntitySnapshotSK int IDENTITY(1,1) NOT NULL 
            ,   EntityID int NOT NULL
            ,   SnapshotTS datetime
            ,   Reputation int
            );
         */

        /// <summary>
        /// Gets or sets the user id for an entity on a stack exchange site
        /// </summary>
        public int EntityID { get; set; }

        /// <summary>
        /// Gets or sets the time in which the crawl was executed
        /// </summary>
        public DateTime SnapshotTS { get; set; }

        /// <summary>
        /// Gets or sets the numeric value for a user's reputation
        /// </summary>
        public int Reputation { get; set; }

        /// <summary>
        /// Gets or sets the last time a user was seen on the site
        /// </summary>
        public DateTime LastSeen { get; set; }

        /// <summary>
        /// Save content to the database
        /// </summary>
        public void Save()
        {
            // HACK: Probably a better way to do this
            Properties.Settings settings = new Properties.Settings();
            string connectionString = string.Empty;
            connectionString = settings.ConnectionManager;

            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
            {
                try
                {
                    System.Data.SqlClient.SqlCommand command = null;

                    connection.Open();
                    command = new System.Data.SqlClient.SqlCommand("dbo.EntitySnapshotAdd");
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Connection = connection;

                    System.Data.SqlClient.SqlParameterCollection parameters = null;
                    parameters = command.Parameters;
                    System.Data.SqlClient.SqlParameter p = null;

                    p = new System.Data.SqlClient.SqlParameter("@EntityID", System.Data.SqlDbType.Int);
                    p.Value = this.EntityID;
                    parameters.Add(p);

                    p = new System.Data.SqlClient.SqlParameter("@SnapshotTS", System.Data.SqlDbType.DateTime);
                    p.Value = this.SnapshotTS;
                    parameters.Add(p);

                    p = new System.Data.SqlClient.SqlParameter("@Reputation", System.Data.SqlDbType.Int);
                    p.Value = this.Reputation;
                    parameters.Add(p);

                    p = new System.Data.SqlClient.SqlParameter("@LastSeen", System.Data.SqlDbType.DateTime);
                    p.Value = this.LastSeen;
                    parameters.Add(p);              
      
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
    }
}
